package com.cssy.yd.logic.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.yd.logic.dao.LogicOtherOutwhDAO;
import com.cssy.yd.logic.dao.LogicOtherOutwhItemBatchDAO;
import com.cssy.yd.logic.dao.LogicOtherOutwhItemDAO;
import com.cssy.yd.logic.entity.LogicOtherInwhItemBatchDO;
import com.cssy.yd.logic.entity.LogicOtherInwhItemDO;
import com.cssy.yd.logic.entity.LogicOtherOutwhDO;
import com.cssy.yd.logic.entity.LogicOtherOutwhItemBatchDO;
import com.cssy.yd.logic.entity.LogicOtherOutwhItemDO;
import com.cssy.yd.logic.entity.LogicWhAffDO;
import com.cssy.yd.logic.service.ILogicOtherOutwhService;
import com.cssy.yd.logic.service.ILogicWhAffStockService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.SyCodeType;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicOtherOutwhService")
public class LogicOtherOutwhServiceImpl implements ILogicOtherOutwhService{

	@Resource(name = "logicOtherOutwhItemDAO")
	private LogicOtherOutwhItemDAO logicOtherOutwhItemDAO;
	
	@Resource(name = "logicOtherOutwhDAO")
	private LogicOtherOutwhDAO logicOtherOutwhDAO;
	
	@Resource(name = "logicWhAffStockService")
	private ILogicWhAffStockService logicWhAffStockService;
	
	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Resource(name="logicOtherOutwhItemBatchDAO")
	private LogicOtherOutwhItemBatchDAO logicOtherOutwhItemBatchDAO;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs=new SearchResult(scs);
		List<LogicOtherOutwhDO>  list= logicOtherOutwhDAO.find(scs);
		rs.setRecordCount(logicOtherOutwhDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		LogicOtherOutwhDO basic = new LogicOtherOutwhDO();
		basic.setDutime(new Date());
		basic.setDuuser(user.getName());
		basic.setId(id);
		basic.setRemoved(1);
		logicOtherOutwhItemDAO.deleteByPid(id);
		return logicOtherOutwhDAO.updateLogicOtherOutwhDO(basic);
	}

	@Override
	public Integer update(LogicOtherOutwhDO t, User user)
			throws SyBaseException {
		t.setDuuser(user.getName());
		t.setDutime(new Date());
		return logicOtherOutwhDAO.updateLogicOtherOutwhDO(t);
	}

	@Override
	public LogicOtherOutwhDO save(LogicOtherOutwhDO t, User user)
			throws SyBaseException {
		Date date=new Date();
		t.setDcruser(user.getName());
		t.setDctime(date);
		logicOtherOutwhDAO.insertLogicOtherOutwhDO(t);
		return t;
	}

	@Override
	public LogicOtherOutwhDO findListById(String id) throws SyBaseException {
		return logicOtherOutwhDAO.findLogicOtherOutwhDOByPrimaryKey(id);
	}

//	@Override
//	public Integer update(LogicOtherOutwhDO curAP,
//			List<LogicOtherOutwhItemDO> listpi, User user)
//			throws SyBaseException {
//		LogicOtherOutwhDO loi = logicOtherOutwhDAO.findLogicOtherOutwhDOByPrimaryKey(curAP.getId());
//		logicOtherOutwhItemDAO.deleteByPid(curAP.getId());
//		for (LogicOtherOutwhItemDO lii : listpi){
//			String id = IDCodeTypeUtil.getId();
//			lii.setId(id);
//			lii.setOtherOutwhId(curAP.getId());
//			logicOtherOutwhItemDAO.insertLogicOtherOutwhItemDO(lii);
//			if (curAP.getBillStatus() == 3){
//				LogicWhAffDO lw = new LogicWhAffDO();
//				lw.setBillCode(loi.getOutwhCode());
//				lw.setBatch(loi.getBatch());
//				lw.setBillType("QC");
//				lw.setCode(lii.getCode());
//				lw.setDcruser(user.getName());
//				lw.setDctime(new Date());
//				lw.setName(lii.getName());
//				lw.setQuantity(-lii.getQuantity());
//				lw.setSpec(lii.getSpec());
//				lw.setUnit(lii.getUnit());
//				lw.setStatus("1");
//				lw.setBrand(lii.getBrand());
//				lw.setWhCode(loi.getWhCode());
//				lw.setHubName(curAP.getWhName());
//				logicWhAffStockService.updateWhAffstockQuantity(lw, user,false);
//
//			}
//		}
//		
//		return this.update(curAP, user);
//	}

//	@Override
//	public LogicOtherOutwhDO save(LogicOtherOutwhDO curAP,List<LogicOtherOutwhItemDO> listpi, User user) throws SyBaseException {
//		for (LogicOtherOutwhItemDO lii : listpi){
//			String id = IDCodeTypeUtil.getId();
//			lii.setId(id);
//			lii.setOtherOutwhId(curAP.getId());
//			logicOtherOutwhItemDAO.insertLogicOtherOutwhItemDO(lii);
//		}
//		return this.save(curAP, user);
//	}

	@Override
	public void outSave(LogicOtherOutwhDO curAP,HttpServletRequest request) throws SyBaseException {
		try{
			String json = request.getParameter("addressInfos");
			List<LogicOtherOutwhItemDO> axxlist = JSONArray.parseArray(json,LogicOtherOutwhItemDO.class);
			if(curAP.getId()!=null && !"".equals(curAP.getId())){//修改
				int rt=0;
				rt=this.update(curAP, SessionUtil.getUser(request));
				if(0==rt){
					throw new SyBaseException("您修改的行数据发生改变！请重新打开查看");
				}
				if(curAP.getBillStatus() != 3){
//					List<LogicOtherOutwhItemDO> list= logicOtherOutwhItemDAO.findByPid(curAP.getId());
//					for (int i = 0; i < list.size(); i++) {
//						logicOtherOutwhItemBatchDAO.deleteByotherOutwhItemId(list.get(i).getId());
//					}
					logicOtherOutwhItemDAO.deleteByPid(curAP.getId());
					for (LogicOtherOutwhItemDO lii : axxlist){
						String id = IDCodeTypeUtil.getId();
						lii.setId(id);
						lii.setOtherOutwhId(curAP.getId());
						logicOtherOutwhItemDAO.insertLogicOtherOutwhItemDO(lii);
//						if(lii.getItemBatch() != null && !"".equals(lii.getItemBatch())){
//							List<LogicOtherOutwhItemBatchDO> itemBatchDOs=JSONArray.parseArray(lii.getItemBatch(), LogicOtherOutwhItemBatchDO.class);
//							if(itemBatchDOs != null && itemBatchDOs.size() > 0){
//								for (int i = 0; i < itemBatchDOs.size(); i++) {
//									itemBatchDOs.get(i).setId(IDCodeTypeUtil.getId());
//									itemBatchDOs.get(i).setOtherOutwhItemId(id);
//									logicOtherOutwhItemBatchDAO.insertLogicOtherOutwhItemBatchDO(itemBatchDOs.get(i));
//								}
//							}
//						}
					}
				}
				if (curAP.getBillStatus() == 3){
					LogicOtherOutwhDO loi = logicOtherOutwhDAO.findLogicOtherOutwhDOByPrimaryKey(curAP.getId());
					for (LogicOtherOutwhItemDO lii : axxlist){
						if (curAP.getBillStatus() == 3){
//							if(lii.getItemBatch() != null && !"".equals(lii.getItemBatch())){
//								List<LogicOtherOutwhItemBatchDO> itemBatchDOs=JSONArray.parseArray(lii.getItemBatch(), LogicOtherOutwhItemBatchDO.class);
//								if(itemBatchDOs != null && itemBatchDOs.size() > 0){
//									for (int i = 0; i < itemBatchDOs.size(); i++) {
									LogicWhAffDO lw = new LogicWhAffDO();
									lw.setBillCode(loi.getOutwhCode());
									lw.setBatch(lii.getBatch());
									lw.setBillType("QC");
									lw.setCode(lii.getCode());
									lw.setDcruser(SessionUtil.getUser(request).getName());
									lw.setDctime(new Date());
									lw.setName(lii.getName());
									lw.setQuantity(-lii.getQuantity());
									lw.setSpec(lii.getSpec());
									lw.setStatus(lii.getStatus());
									lw.setBrand(lii.getBrand());
									lw.setHubName(curAP.getWhName());
									lw.setWhCode(loi.getWhCode());
									lw.setWhname(loi.getWhName());
									lw.setUnit(lii.getUnit());
									lw.setJunit(lii.getJunit());
									lw.setJquantity(-lii.getJquantity());
									lw.setBzq(lii.getBzq());
									lw.setYxq(lii.getYxq());
									lw.setScrq(lii.getScrq());
									logicWhAffStockService.updateWhAffstockQuantity(lw, SessionUtil.getUser(request),false);
//									}
//								}
//							}
						}
					}
				}
			}else{
					String pcode=syCodeService.getCode("QC","QC",SyCode.DATE_YYMM,5);
					curAP.setOutwhCode(pcode);
					String id = IDCodeTypeUtil.getId();
					curAP.setId(id);
					LogicOtherOutwhDO cdo=this.save(curAP, SessionUtil.getUser(request));
					for (LogicOtherOutwhItemDO lii : axxlist){
						String itemId = IDCodeTypeUtil.getId();
						lii.setId(itemId);
						lii.setOtherOutwhId(id);
						logicOtherOutwhItemDAO.insertLogicOtherOutwhItemDO(lii);
//						if(lii.getItemBatch() != null && !"".equals(lii.getItemBatch())){
//							List<LogicOtherOutwhItemBatchDO> itemBatchDOs=JSONArray.parseArray(lii.getItemBatch(), LogicOtherOutwhItemBatchDO.class);
//							if(itemBatchDOs != null && itemBatchDOs.size() > 0){
//								for (int i = 0; i < itemBatchDOs.size(); i++) {
//									itemBatchDOs.get(i).setId(IDCodeTypeUtil.getId());
//									itemBatchDOs.get(i).setOtherOutwhItemId(itemId);
//									logicOtherOutwhItemBatchDAO.insertLogicOtherOutwhItemBatchDO(itemBatchDOs.get(i));
//								}
//							}
//						}
					}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()
					+ e.getMessage());
		}
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		// TODO Auto-generated method stub
		return null;
	}

}
